<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tab选项卡</title>
  <link rel="stylesheet" href="tabs.css">
</head>

<body>
  <div class="container">
    <h2>Tab选项卡</h2>
    <div class="tabs">
      <div class="tabs-header">
        <span class="tabs-items active">用户管理</span>
        <span class="tabs-items">配置管理</span>
        <span class="tabs-items">角色管理</span>
        <span class="tabs-items">定时任务补偿</span>
        <span class="line"></span>
      </div>
      <div class="tab-content">
        <div class="tabs-son active">用户管理</div>
        <div class="tabs-son">配置管理</div>
        <div class="tabs-son">角色管理</div>
        <div class="tabs-son">定时任务补偿</div>
      </div>
    </div>
  </div>
  <div class="container">
    <h2>Tab选项卡</h2>
    <div class="tabs tabs2">
      <div class="tabs-header">
        <span class="tabs-items active">用户管理</span>
        <span class="tabs-items">配置管理</span>
        <span class="tabs-items">角色管理</span>
        <span class="tabs-items">定时任务补偿</span>
        <span class="line"></span>
      </div>
      <div class="tab-content">
        <div class="tabs-son active">用户管理</div>
        <div class="tabs-son">配置管理</div>
        <div class="tabs-son">角色管理</div>
        <div class="tabs-son">定时任务补偿</div>
      </div>
    </div>
  </div>
  <script>
    class Tabs {
      constructor($tabs) {
        this.$tabs = $tabs
        this.$$items = $tabs.querySelectorAll('.tabs-items')
        this.$$son = $tabs.querySelectorAll('.tabs-son')
        this.$line = $tabs.querySelector('.line')

        this.init()
        this.bind()
      }
      init() {
        this.$line.style.width = this.$$items[0].offsetWidth + 'px'
      }

      bind() {
        this.$$items.forEach(a => {
          let self = this
          a.onclick = function () {
            self.$$items.forEach(a => a.classList.remove('active'))
            this.classList.add('active')
            self.$line.style.width = this.offsetWidth + 'px'
            self.$line.style.transform = `translateX(${this.offsetLeft}px)`

            let index = Array.from(self.$$items).indexOf(this)
            self.$$son.forEach(a => a.classList.remove('active'))
            self.$$son[index].classList.add('active')

          }
        })

      }

}
    new Tabs(document.querySelector('.tabs'))
    new Tabs(document.querySelector('.tabs2'))
  </script>
</body>

</html>